/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.query.bindingtree.visitor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import edu.ucsd.db.bassarisk.query.binding.BindingTuple;
import edu.ucsd.db.bassarisk.query.binding.Bindings.Binding;
import edu.ucsd.db.bassarisk.query.binding.Bindings.BindingCombination;
import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.BindingTreeEdge;
import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.ChoiceNode;
import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.ChoiceNode.Choice;
import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.ChoiceNode.ChooseAll;
import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.ChoiceNode.ChooseOne;
import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.EstablishingNode;

public class BindingTupleMapper implements BindingTreeVisitor<List<Binding>, Void> {
  private final Map<BindingTreeEdge, BindingTuple> bindingTuples;

  public BindingTupleMapper(final Map<BindingTreeEdge, BindingTuple> bindingTuples) {
    this.bindingTuples = bindingTuples;
  }
  
  private List<Binding> findParents(List<Binding> bindings) {
    List<Binding> parents = new ArrayList<Binding>();
    for (Binding binding : bindings) {
      if (binding.inUnion() || binding.inProduct()) {
        Binding parent = binding.getParent();
        if (!bindings.contains(parent) && !parents.contains(parent)) {
          boolean allIn = true; // assume this can be added, show otherwise
          for (Binding child : ((BindingCombination) parent).getCombinedBindings()) {
            if (!bindings.contains(child)) {
              allIn = false;
              break;
            }
          }
          if (allIn) {
            parents.add(parent);
          }
        }
      }
    }
    return parents;
  }

  @Override
  public List<Binding> visitBindingTreeEdge(final BindingTreeEdge edge,
                                            final Void args) {
    List<Binding> bindings = new ArrayList<Binding>();
    bindings.addAll(edge.tailNode().accept(this, null));
    // Add parents that make since until the list converges.
    int preExpansionSize;
    do {
      preExpansionSize = bindings.size();
      bindings.addAll(findParents(bindings));
    } while (bindings.size() > preExpansionSize);
    bindings.addAll(edge.computations());
    this.bindingTuples.put(edge, new BindingTuple(bindings));
    return bindings;
  }

  @Override
  public List<Binding> visitChoiceNode(final ChoiceNode choices, final Void args) {
    List<Binding> bindings = new ArrayList<Binding>();
    for (Choice choice : choices.getChoices()) {
      if (choice instanceof ChooseOne) {
        bindings.addAll(((ChooseOne) choice).getChild().accept(this, null));
      } else if (choice instanceof ChooseAll) {
        for (BindingTreeEdge edge : ((ChooseAll) choice).getChildren()) {
          bindings.addAll(edge.accept(this, null));
        }
      }
    }
    return bindings;
  }

  @Override
  public List<Binding> visitEstablishingNode(final EstablishingNode node,
                                             final Void args) {
    List<Binding> bindings = new ArrayList<Binding>();
    for (BindingTreeEdge edge : node.children()) {
      bindings.addAll(edge.accept(this, null));
    }
    bindings.add(node.getEstablishedBinding());
    return bindings;
  }
}
